//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AnalyticHestonHullWhiteEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Models/Equity/HestonModel.h>
#include <gen/QL/Models/Shortrate/Onefactormodels/HullWhite.h>
#include <gen/QL/Instruments/TypePayoff.h>
#include <gen/QL/Pricingengines/Vanilla/AnalyticHestonEngine.h>
using namespace Cephei::QL::Models::Equity;
using namespace Cephei::QL::Models::Shortrate::Onefactormodels;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (Cephei::QL::Models::Equity::IHestonModel^ model, Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^ hullWhiteModel, Double relTolerance, UInt64 maxEvaluations) : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
    CHestonModel^ _Cmodel;
    CHullWhite^ _ChullWhiteModel;
    try
    {
#ifdef HANDLE
        _phAnalyticHestonHullWhiteEngine = NULL;
#endif
        _Cmodel = safe_cast<CHestonModel^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::HestonModel>& _model = static_cast<boost::shared_ptr<QuantLib::HestonModel>&> (_Cmodel->GetShared ()); 
        _ChullWhiteModel = safe_cast<CHullWhite^> (hullWhiteModel);
        _ChullWhiteModel->Lock();
        boost::shared_ptr<QuantLib::HullWhite>& _hullWhiteModel = static_cast<boost::shared_ptr<QuantLib::HullWhite>&> (_ChullWhiteModel->GetShared ()); 
        QuantLib::Real _relTolerance = (QuantLib::Real)ValueHelper::Convert (relTolerance); //d
        QuantLib::Size _maxEvaluations = (QuantLib::Size)ValueHelper::Convert (maxEvaluations); //d
        _ppAnalyticHestonHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine> (new QuantLib::AnalyticHestonHullWhiteEngine ( _model,  _hullWhiteModel,  _relTolerance,  _maxEvaluations ));
        SetAnalyticHestonEngine (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cmodel != nullptr) _Cmodel->Unlock();
        if (_ChullWhiteModel != nullptr) _ChullWhiteModel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (Cephei::QL::Models::Equity::IHestonModel^ hestonModel, Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^ hullWhiteModel, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationOrder) : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
    CHestonModel^ _ChestonModel;
    CHullWhite^ _ChullWhiteModel;
    try
    {
#ifdef HANDLE
        _phAnalyticHestonHullWhiteEngine = NULL;
#endif
        _ChestonModel = safe_cast<CHestonModel^> (hestonModel);
        _ChestonModel->Lock();
        boost::shared_ptr<QuantLib::HestonModel>& _hestonModel = static_cast<boost::shared_ptr<QuantLib::HestonModel>&> (_ChestonModel->GetShared ()); 
        _ChullWhiteModel = safe_cast<CHullWhite^> (hullWhiteModel);
        _ChullWhiteModel->Lock();
        boost::shared_ptr<QuantLib::HullWhite>& _hullWhiteModel = static_cast<boost::shared_ptr<QuantLib::HullWhite>&> (_ChullWhiteModel->GetShared ()); 
        QuantLib::Size _integrationOrder = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (integrationOrder) ? (QuantLib::Size)ValueHelper::Convert (integrationOrder->Value) : 144); //4
        _ppAnalyticHestonHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine> (new QuantLib::AnalyticHestonHullWhiteEngine ( _hestonModel,  _hullWhiteModel,  _integrationOrder ));
        SetAnalyticHestonEngine (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_ChestonModel != nullptr) _ChestonModel->Unlock();
        if (_ChullWhiteModel != nullptr) _ChullWhiteModel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine>& childNative, Object^ owner) : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticHestonHullWhiteEngine = NULL;
#endif
	_ppAnalyticHestonHullWhiteEngine = &childNative;
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (QuantLib::AnalyticHestonHullWhiteEngine& childNative, Object^ owner) : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticHestonHullWhiteEngine = NULL;
#endif
	_ppAnalyticHestonHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine> (&childNative);
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
    _AnalyticHestonHullWhiteEngineOwner = owner;
    _AnalyticHestonEngineOwner = owner;
}

Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (CAnalyticHestonHullWhiteEngine^ copy) : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticHestonHullWhiteEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticHestonHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine> (copy->GetShared());
        _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (PLATFORM::Type^ t) : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticHestonHullWhiteEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticHestonHullWhiteEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (QuantLib::Handle<QuantLib::AnalyticHestonHullWhiteEngine>& childNative, Object^ owner)  : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
	_phAnalyticHestonHullWhiteEngine = &childNative;
	_ppAnalyticHestonHullWhiteEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine>>(childNative.currentLink());
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
    _AnalyticHestonHullWhiteEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (QuantLib::Handle<QuantLib::AnalyticHestonHullWhiteEngine> childNative)  : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
	_phAnalyticHestonHullWhiteEngine = &childNative;
	_ppAnalyticHestonHullWhiteEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine>>(childNative.currentLink());
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::CAnalyticHestonHullWhiteEngine (QuantLib::AnalyticHestonHullWhiteEngine childNative)  : CAnalyticHestonEngine(CAnalyticHestonHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticHestonHullWhiteEngine = NULL;
#endif
	_ppAnalyticHestonHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine> (new QuantLib::AnalyticHestonHullWhiteEngine (childNative));
    _ppAnalyticHestonEngine = new boost::shared_ptr<QuantLib::AnalyticHestonEngine> (boost::dynamic_pointer_cast<QuantLib::AnalyticHestonEngine> (*_ppAnalyticHestonHullWhiteEngine));
}
#endif

Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::~CAnalyticHestonHullWhiteEngine ()
{
    if (_ppAnalyticHestonHullWhiteEngine != NULL)
    {
	    delete _ppAnalyticHestonHullWhiteEngine;
        _ppAnalyticHestonHullWhiteEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::!CAnalyticHestonHullWhiteEngine ()
{
    if (_ppAnalyticHestonHullWhiteEngine != NULL)
    {
	    delete _ppAnalyticHestonHullWhiteEngine;
    }
}
QuantLib::AnalyticHestonHullWhiteEngine& Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::GetReference ()
{
    if (_ppAnalyticHestonHullWhiteEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppAnalyticHestonHullWhiteEngine;
}
boost::shared_ptr<QuantLib::AnalyticHestonHullWhiteEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::GetShared ()
{
    if (_ppAnalyticHestonHullWhiteEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppAnalyticHestonHullWhiteEngine;
}
QuantLib::AnalyticHestonHullWhiteEngine* Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::GetPointer ()
{
    if (_ppAnalyticHestonHullWhiteEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppAnalyticHestonHullWhiteEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticHestonHullWhiteEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::GetHandle ()
{
	if (_phAnalyticHestonHullWhiteEngine == NULL)
	{
		_phAnalyticHestonHullWhiteEngine = new Handle<QuantLib::AnalyticHestonHullWhiteEngine> (*_ppAnalyticHestonHullWhiteEngine);
	}
	return *_phAnalyticHestonHullWhiteEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::HasNative () 
{
	return (_ppAnalyticHestonHullWhiteEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IAnalyticHestonHullWhiteEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticHestonHullWhiteEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Pricingengines::Vanilla::IAnalyticHestonHullWhiteEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppAnalyticHestonHullWhiteEngine)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IAnalyticHestonHullWhiteEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine_Factory::Create (Cephei::QL::Models::Equity::IHestonModel^ model, Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^ hullWhiteModel, Double relTolerance, UInt64 maxEvaluations)
{
    return REFNEW CAnalyticHestonHullWhiteEngine ( model,  hullWhiteModel,  relTolerance,  maxEvaluations);
}
Cephei::QL::Pricingengines::Vanilla::IAnalyticHestonHullWhiteEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticHestonHullWhiteEngine_Factory::Create (Cephei::QL::Models::Equity::IHestonModel^ hestonModel, Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^ hullWhiteModel, Microsoft::FSharp::Core::FSharpOption<UInt64>^ integrationOrder)
{
    return REFNEW CAnalyticHestonHullWhiteEngine ( hestonModel,  hullWhiteModel,  integrationOrder);
}
